//
//  MineViewController.m
//  BabyAndBaby
//
//  Created by 范琦 on 16/7/27.
//  Copyright © 2016年 范琦. All rights reserved.
//

#import "MineViewController.h"
#import "MineTopView.h"
#import "MineModel.h"
#import "MineCell.h"
#import "MineTwoWMView.h"
#import "MineWebViewController.h"
#import "MineEditPasswordViewController.h"

@interface MineViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) MineTopView *topView;
@property (nonatomic, strong) UIView *mainView;
@property (nonatomic, strong) MineModel *mineModel;

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *listArr;
@property (nonatomic, strong) NSMutableArray *imgArr;
@property (nonatomic, strong) MineTwoWMView *twoWMView;
@property (nonatomic, strong) UIView *backView;


@end

@implementation MineViewController

- (void)viewWillAppear:(BOOL)animated
{
    [self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
    [self.navigationController.navigationBar setShadowImage:[UIImage new]];

}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.listArr = [NSMutableArray arrayWithObjects:@"修改密码", @"二维码", @"常见问题", @"关于我们", @"退出登录", nil];
    self.imgArr = [NSMutableArray arrayWithObjects:@"填写", @"二维码", @"常见问题 ", @"关于", @"退出 ", nil];

    self.mainView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, KSCREENWIDTH, KSCREENHEIGHT)];
    _mainView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_mainView];
    self.title = @"我的";
    [self creatMineTopView];
    [self netHandle];
    [self creatTableView];
}

- (void)netHandle
{
    YTKNetHandle *api = [[YTKNetHandle alloc]initWithUrl:@"/Recommend/my.html" paramDic:@{@"token":TOKEN} RegisterApiMethod:RequestMethodGet];
    
    [api startWithCompletionBlockWithSuccess:^(YTKBaseRequest *request) {
        
        self.mineModel = [[MineModel alloc]init];
        [_mineModel setValuesForKeysWithDictionary:request.responseBody[@"result"]];
        
        [_topView.userImg sd_setImageWithURL:[NSURL URLWithString:_mineModel.head_photo] placeholderImage:nil];
        _topView.label.text = _mineModel.name;
        NSLog(@"%@", request.responseBody);
        
    } failure:^(YTKBaseRequest *request) {
        
    }];
}

- (void)creatTwoWMView
{
    self.backView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, KSCREENWIDTH, KSCREENHEIGHT)];
    _backView.backgroundColor = [UIColor blackColor];
    _backView.alpha = 0.2;
    [[[UIApplication sharedApplication].delegate window] addSubview:_backView];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]init];
    [_backView addGestureRecognizer:tap];
    [[tap rac_gestureSignal] subscribeNext:^(id x) {
       
        [_backView removeFromSuperview];
        [_twoWMView removeFromSuperview];
        
    }];
    
    
    self.twoWMView = [[[NSBundle mainBundle] loadNibNamed:@"MineTwoWMView" owner:nil options:nil] lastObject];
    _twoWMView.frame = CGRectMake(43, 60 + 64, KSCREENWIDTH - 86, 337);
    [[[UIApplication sharedApplication].delegate window] addSubview:_twoWMView];

}

- (void)creatMineTopView
{
    self.topView = [[[NSBundle mainBundle] loadNibNamed:@"MineTopView" owner:nil options:nil] lastObject];
    _topView.frame = CGRectMake(0, 0, KSCREENWIDTH, 160);
    _topView.backgroundColor = BACKGROUNDCOLOR;
    _topView.label.textColor = [UIColor whiteColor];
    [self.mainView addSubview:_topView];
}

- (void)creatTableView
{
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 150, KSCREENWIDTH, KSCREENHEIGHT - 160) style:UITableViewStyleGrouped];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self.mainView addSubview:_tableView];
    _tableView.scrollEnabled = NO;
    _tableView.sectionFooterHeight = 0;
    _tableView.sectionHeaderHeight = 15;
    _tableView.backgroundColor = HUIBACKGROUNDCOLOR;
    _tableView.rowHeight = 44;
    [_tableView registerNib:[UINib nibWithNibName:@"MineCell" bundle:nil] forCellReuseIdentifier:@"cell"];
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;


}
#warning TableView的代理方法
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 15;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 5;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

// Row display. Implementers should *always* try to reuse cells by setting each cell's reuseIdentifier and querying for available reusable cells with dequeueReusableCellWithIdentifier:
// Cell gets various attributes set automatically based on table (separators) and data source (accessory views, editing controls)

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MineCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.label.text = _listArr[indexPath.section];
    cell.Img.image = [UIImage imageNamed:_imgArr[indexPath.section]];
    
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 1) {
        [self creatTwoWMView];
    }
    if (indexPath.section == 2) {
        
        MineWebViewController *webVC = [[MineWebViewController alloc]init];
        webVC.str = self.mineModel.qa;
        webVC.title = @"常见问题";
        [self.navigationController pushViewController:webVC animated:YES];
    }
    if (indexPath.section == 3) {
        MineWebViewController *webVC = [[MineWebViewController alloc]init];
        webVC.str = self.mineModel.about;
        webVC.title = @"关于我们";
        [self.navigationController pushViewController:webVC animated:YES];
    }
    if (indexPath.section == 0) {
        MineEditPasswordViewController *editVC = [[MineEditPasswordViewController alloc]init];
        [self.navigationController pushViewController:editVC animated:YES];
    }
    if (indexPath.section == 4) {
        [[NSNotificationCenter defaultCenter] postNotificationName:@"back" object:nil];
    }
}

//在页面消失的时候就让navigationbar还原样式
-(void)viewWillDisappear:(BOOL)animated{
    
    [self.navigationController.navigationBar setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];
    [self.navigationController.navigationBar setShadowImage:nil];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
